package service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.DishAuditDao;
import dao.LowAuditDao;
import dao.MerchantAuditDao;
import po.LowAudit;
import po.MerchantAudit;
import service.LowAuditManager;
import service.MerchantAuditManager;
import service.WebService;
import vo.MerchantAuditVo;
import vo.Merchant;

@Service("lowAuditManager")
public class LowAuditManagerImpl implements LowAuditManager{

	@Autowired
	private LowAuditDao lad;
	@Autowired
	private MerchantAuditDao mad;
	@Autowired
	private WebService ws;
	
	@Override
	public List<MerchantAuditVo> listAllLowAudit() {
		// TODO Auto-generated method stub
		List<LowAudit> lowAudits = lad.listAllLowAudit();
		List<String> midList = new ArrayList<String>();
		List<MerchantAuditVo> merchantList = new ArrayList<MerchantAuditVo>();
		for (LowAudit lowAudit : lowAudits) {
			midList.add(lowAudit.getmId());
		}
		Map<String, Merchant> map = ws.listMerchantByMId(midList);
		for (LowAudit lowAudit_ : lowAudits) {
			merchantList.add(new MerchantAuditVo(map.get(lowAudit_.getmId()), 
					lowAudit_.getStatus(), lowAudit_.getCreateDate()));
		}
		return merchantList;
	}

	@Override
	public LowAudit findLowAuditById(String id) {
		// TODO Auto-generated method stub
		return lad.findLowAuditById(id);
	}

	@Override
	public String addLowAudit(LowAudit lowAudit) {
		// TODO Auto-generated method stub
		LowAudit lowAudit_ = lad.findLowAuditById(lowAudit.getmId());
		if(lowAudit_ != null){
			lowAudit_.setCreateDate(lowAudit.getCreateDate());
			lowAudit_.setStatus(0);
			return lad.updateLowAuditStatus(lowAudit_);
		} else {
			return lad.insertlowAudit(lowAudit);
		}
	}

	@Override
	public String updateLowAuditStatus(String id, int status) {
		// TODO Auto-generated method stub
		LowAudit lowAudit = lad.findLowAuditById(id);
		MerchantAudit merchantAudit = mad.findMerchantAuditById(id);
		if(lowAudit != null && merchantAudit != null){
			lowAudit.setStatus(1);
			lad.updateLowAuditStatus(lowAudit);
			merchantAudit.setStatus(status);
			mad.updateMerchantAudit(merchantAudit);
			return lowAudit.getmId();
		}else {
			return 0 + "";
		}
	}

}
